package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.client.cmn.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-12-18
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;


    //根据id查询
    @Override
    public Patient getPatientId(Long id) {
        Patient patient = baseMapper.selectById(id);

        return patient;
    }

    @Override
    public List<Patient> findAll(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        patientList.stream().forEach(item ->{
            this.packagePatient(item);
        });
        return patientList;
    }

    private Patient packagePatient(Patient patient) {

        //获取值
        String provinceCode = patient.getProvinceCode();
        String cityCode = patient.getCityCode();
        String districtCode = patient.getDistrictCode();

        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件

         //远程调用
        String provienceName = dictFeignClient.getName(provinceCode);
        String cityName = dictFeignClient.getName(cityCode);
        String districtName = dictFeignClient.getName(districtCode);


        //封装
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("provinceString", provienceName);
        patient.getParam().put("cityString", cityName);
        patient.getParam().put("districtString", districtName);
        patient.getParam().put("fullAddress", provienceName + cityName + districtName + patient.getAddress());

        return patient;
    }
}
